package com.lbs.droiddoctor.disease;

import java.util.ArrayList;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lbs.droiddoctor.R;
import com.lbs.droiddoctor.base.BaseActivity;
import com.lbs.droiddoctor.common.HttpASync;
import com.lbs.droiddoctor.common.HttpASync.OnRequest;
import com.lbs.droiddoctor.constants.AppConstants;
import com.lbs.view.adapter.DiseaseAdapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ListView;
import android.widget.Toast;

public class ShowDisease extends BaseActivity implements OnRequest {

	private ListView diseaseList;
	private DiseaseAdapter adapter;

	private ArrayList<Disease> diseaseListData;
	private Context ctx;

	private Handler uiHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			if (msg.what == 1) {
				SharedPreferences pref = getSharedPreferences(
						AppConstants.PREF_FILE, 0);
				String uName = pref.getString("username", null);
				if (uName.equals("admin")) {
					int position = msg.arg1;
					String disease = diseaseListData.get(position).getDiseaseName();
					String diseasePathUrl = AppConstants.DELETE_DISEASE_PATH + "&disease="+disease;
					HttpASync request = new HttpASync();					
					request.execute(diseasePathUrl);
					diseaseListData.remove(position);

					runOnUiThread(new Runnable() {

						@Override
						public void run() {
							adapter.notifyDataSetChanged();

						}
					});
				} else {
					runOnUiThread(new Runnable() {

						@Override
						public void run() {
							Toast.makeText(
									ctx,
									"Your privilages doesn't allows you to delete the disease",
									Toast.LENGTH_LONG).show();

						}
					});
				}
			} else if (msg.what == 2) {
				final int position = msg.arg1;

				runOnUiThread(new Runnable() {

					@Override
					public void run() {

						Intent intent = new Intent(ctx, AddDisease.class);
						intent.putExtra("Disease",
								diseaseListData.get(position));
						ctx.startActivity(intent);
					}
				});

			}

		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.ctx = this;
		setContentView(R.layout.update_disease);
		diseaseList = (ListView) findViewById(R.id.listView1);

		// adapter = new DiseaseAdapter(this, diseases, uiHandler);
		// diseaseList.setAdapter(adapter);

	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		String diseasePathUrl = AppConstants.DISEASE_PATH + "?method=get";
		HttpASync request = new HttpASync();
		request.setOnRequestListener(this);
		request.execute(diseasePathUrl);
		showProgress("Loading...");
	}

	@Override
	public void onResponseReceived(String response) {
		Gson gson = new Gson();

		diseaseListData = gson.fromJson(response,
				new TypeToken<List<Disease>>() {
				}.getType());
		final ArrayList<Disease> diseaselist = diseaseListData;
		runOnUiThread(new Runnable() {

			@Override
			public void run() {
				adapter = new DiseaseAdapter(ctx, diseaselist, uiHandler);
				diseaseList.setAdapter(adapter);
				hideProgress();
			}
		});
	}

}
